package org.huangrui.spark.scala.core.acc

import org.apache.spark.{SparkConf, SparkContext}

import scala.collection.mutable

/**
 * @Author hr
 * @Create 2024-10-20 0:20
 */
object Spark06_Bc {
  def main(args: Array[String]): Unit = {
    val sparConf = new SparkConf().setMaster("local").setAppName("bc")
    val sc = new SparkContext(sparConf)

    val rdd1 = sc.makeRDD(List(
      ("a", 1), ("b", 2), ("c", 3), ("b", 4)
    ))
    val map = mutable.Map(("a", 4), ("b", 5), ("c", 6))

    // 封装广播变量
    val bc = sc.broadcast(map)

    val mapRdd = rdd1.map {
      case (k, v) => {
        // 调用广播变量
        val value = bc.value.getOrElse(k, 0)
        (k, (v, value))
      }
    }

    mapRdd.collect().foreach(println)
    sc.stop()
  }
}
